package com.jsrana.idea.plugin.ideaweather.ui;

import com.jsrana.idea.plugin.ideaweather.IdeaWeatherPlugin;
import com.jsrana.idea.plugin.ideaweather.util.ImageUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class WeatherDialog
        extends JDialog {
    private JPanel contentPane;
    private JLabel label_temperature;
    private JLabel label_weathericon;
    private JLabel label_location;
    private JLabel label_condition;
    private JLabel label_forecast_0_day;
    private JLabel label_forecast_1_day;
    private JLabel label_forecast_2_day;
    private JLabel label_forecast_3_day;
    private JLabel label_forecast_4_day;
    private JLabel label_forecast_0_icon;
    private JLabel label_forecast_1_icon;
    private JLabel label_forecast_2_icon;
    private JLabel label_forecast_3_icon;
    private JLabel label_forecast_4_icon;
    private JLabel label_forecast_0_high;
    private JLabel label_forecast_0_low;
    private JLabel label_forecast_1_high;
    private JLabel label_forecast_2_high;
    private JLabel label_forecast_3_high;
    private JLabel label_forecast_4_high;
    private JLabel label_forecast_1_low;
    private JLabel label_forecast_2_low;
    private JLabel label_forecast_3_low;
    private JLabel label_forecast_4_low;
    private JLabel label_sunrise;
    private JLabel label_sunset;
    private JLabel label_options;
    private JLabel label_condition_text;

    private static final int FORECAST_ICON_SIZE = 32;
    public static final int WIDTH = 375;
    public static final int HEIGHT = 225;

    Font font_roboto_bold;
    Font font_roboto_regular;
    Font font_roboto_thin;

    public WeatherDialog() {
        setContentPane( contentPane );
        setModal( false );

        try {
            font_roboto_bold = FontUIResource.createFont( Font.TRUETYPE_FONT, getClass().getResourceAsStream( "/resources/font/Roboto-Bold.ttf" ) );
            font_roboto_regular = FontUIResource.createFont( Font.TRUETYPE_FONT, getClass().getResourceAsStream( "/resources/font/Roboto-Regular.ttf" ) );
            font_roboto_thin = FontUIResource.createFont( Font.TRUETYPE_FONT, getClass().getResourceAsStream( "/resources/font/Roboto-Thin.ttf" ) );

            if ( font_roboto_bold != null ) {
                label_location.setFont( font_roboto_bold.deriveFont( Font.BOLD, 24 ) );

                Font font_roboto_bold_16 = font_roboto_bold.deriveFont( Font.PLAIN, 16 );
                label_forecast_0_day.setFont( font_roboto_bold_16 );
                label_forecast_1_day.setFont( font_roboto_bold_16 );
                label_forecast_2_day.setFont( font_roboto_bold_16 );
                label_forecast_3_day.setFont( font_roboto_bold_16 );
                label_forecast_4_day.setFont( font_roboto_bold_16 );
            }
            if ( font_roboto_thin != null ) {
                label_temperature.setFont( font_roboto_thin.deriveFont( Font.PLAIN, 72 ) );
            }
            if ( font_roboto_regular != null ) {
                label_condition.setFont( font_roboto_regular.deriveFont( Font.PLAIN, 14 ) );
                label_condition_text.setFont( font_roboto_regular.deriveFont( Font.PLAIN, 12 ) );

                Font font_roboto_regular_10 = font_roboto_regular.deriveFont( Font.PLAIN, 10 );
                label_sunrise.setFont( font_roboto_regular_10 );
                label_sunset.setFont( font_roboto_regular_10 );
            }
        }
        catch ( FontFormatException e ) {
            e.printStackTrace();
        }
        catch ( IOException e ) {
            e.printStackTrace();
        }

        label_options.addMouseListener( new MouseAdapter() {
            @Override public void mouseClicked( MouseEvent e ) {
                OptionsDialog optionsDialog = new OptionsDialog();
                optionsDialog.pack();
                optionsDialog.setLocationRelativeTo( null );
                optionsDialog.setVisible( true );
            }
        } );

        updateUI();
    }

    public void updateUI() {
        Map<String, String> w = IdeaWeatherPlugin.weather;
        label_temperature.setText( w.get( "condition_temp" ) + "°" );

        label_location.setText( w.get( "location_city" ) );
        label_weathericon.setIcon( getIcon( w.get( "condition_code" ), 96 ) );
        label_sunrise.setText( "Sunrise: " + w.get( "astronomy_sunrise" ) );
        label_sunset.setText( "Sunset: " + w.get( "astronomy_sunset" ) );

        List<Map<String, String>> forecast = IdeaWeatherPlugin.forecast;
        if ( forecast != null && forecast.size() == 5 ) {
            label_condition.setText( "H: " + forecast.get( 0 ).get( "forecast_high" ) + "°" + " / L: " + forecast.get( 0 ).get( "forecast_low" ) + "°" );
            label_condition_text.setText( forecast.get( 0 ).get( "forecast_text" ) );
            label_forecast_0_day.setText( forecast.get( 0 ).get( "forecast_day" ) );
            label_forecast_1_day.setText( forecast.get( 1 ).get( "forecast_day" ) );
            label_forecast_2_day.setText( forecast.get( 2 ).get( "forecast_day" ) );
            label_forecast_3_day.setText( forecast.get( 3 ).get( "forecast_day" ) );
            label_forecast_4_day.setText( forecast.get( 4 ).get( "forecast_day" ) );

            label_forecast_0_icon.setIcon( getIcon( forecast.get( 0 ).get( "forecast_code" ), FORECAST_ICON_SIZE ) );
            label_forecast_1_icon.setIcon( getIcon( forecast.get( 1 ).get( "forecast_code" ), FORECAST_ICON_SIZE ) );
            label_forecast_2_icon.setIcon( getIcon( forecast.get( 2 ).get( "forecast_code" ), FORECAST_ICON_SIZE ) );
            label_forecast_3_icon.setIcon( getIcon( forecast.get( 3 ).get( "forecast_code" ), FORECAST_ICON_SIZE ) );
            label_forecast_4_icon.setIcon( getIcon( forecast.get( 4 ).get( "forecast_code" ), FORECAST_ICON_SIZE ) );

            label_forecast_0_high.setText( forecast.get( 0 ).get( "forecast_high" ) + "°" );
            label_forecast_1_high.setText( forecast.get( 1 ).get( "forecast_high" ) + "°" );
            label_forecast_2_high.setText( forecast.get( 2 ).get( "forecast_high" ) + "°" );
            label_forecast_3_high.setText( forecast.get( 3 ).get( "forecast_high" ) + "°" );
            label_forecast_4_high.setText( forecast.get( 4 ).get( "forecast_high" ) + "°" );

            label_forecast_0_low.setText( forecast.get( 0 ).get( "forecast_low" ) + "°" );
            label_forecast_1_low.setText( forecast.get( 1 ).get( "forecast_low" ) + "°" );
            label_forecast_2_low.setText( forecast.get( 2 ).get( "forecast_low" ) + "°" );
            label_forecast_3_low.setText( forecast.get( 3 ).get( "forecast_low" ) + "°" );
            label_forecast_4_low.setText( forecast.get( 4 ).get( "forecast_low" ) + "°" );
        }
    }

    private ImageIcon getIcon( String code,
                               int size ) {
        return getIcon( code, size, size );
    }

    private ImageIcon getIcon( String code,
                               int width,
                               int height ) {
        BufferedImage image = null;
        try {
            if ( code.length() == 1 ) {
                code = "0" + code;
            }
            image = ImageUtil.resize( ImageIO.read( getClass().getResource( "/resources/plain/colorful/" + code + ".png" ) ), width, height );
        }
        catch ( IOException e ) {
            e.printStackTrace();
        }
        return image != null ? new ImageIcon( image ) : null;
    }
}
